/**
 * 
 */
package com.gallery;


import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import com.Adapters.UploadAdapter;
import com.Adapters.Items.ActionsUpload;
import com.Adapters.Items.ItemUpload;
import com.Data.Post;
import com.shareup.R;
import com.utils.AppConstant;
import com.utils.Utils;


/**
 * @author Chris
 * 
 */
public class ActivityGallery extends Activity implements ActionsUpload {

    private int count;

    private boolean[] thumbnailsselection;

    private String[] arrPath;

    private Bitmap[] thumbnails;

    private UploadAdapter adapter;

    private GridView gridView;

    private int columnWidth;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        gridView = (GridView) findViewById(R.id.list_thumb);
        this.InitilizeGridLayout();
        //
        final ImageView selectBtn = (ImageView) findViewById(R.id.button_done);
        selectBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub
                final int len = thumbnailsselection.length;
                int cnt = 0;
                String selectImages = "";
                for (int i = 0; i < len; i++) {
                    if (thumbnailsselection[i]) {
                        cnt++;
                        selectImages = selectImages + arrPath[i]
                                    + "|";
                    }
                }
                if (cnt == 0) {
                    Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
                }
                else {
                    Toast.makeText(getApplicationContext(), "You've selected Total "
                                + cnt + " image(s).", Toast.LENGTH_LONG).show();
                    Log.e("SelectedImages", selectImages);
                }
            }
        });

        getPaths();
    }

    @SuppressWarnings("deprecation")
    private void getPaths() {

        final String[] columns = { MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        this.thumbnails = new Bitmap[this.count];
        this.arrPath = new String[this.count];
        this.thumbnailsselection = new boolean[this.count];
        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
            arrPath[i] = imagecursor.getString(dataColumnIndex);
        }

        fillAdapter();
    }

    private void fillAdapter() {

        adapter = new UploadAdapter(this, R.layout.item_album, columnWidth, this);
        //
        int index = 0;
        for (String path : this.arrPath) {
            Post p = new Post();
            p.setPathFull(path);
            p.setID(index);
            p.setCover(false);
            p.setThumb(thumbnails[index]);
            adapter.add(p);
            index++;

        }

        gridView.setAdapter(adapter);
    }

    private void InitilizeGridLayout() {

        Resources r = getResources();
        float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, AppConstant.GRID_PADDING, r.getDisplayMetrics());

        Utils utils = new Utils(this);

        columnWidth = (int) ((utils.getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);
        gridView.setNumColumns(AppConstant.NUM_OF_COLUMNS_UPLOAD);
        gridView.setColumnWidth(columnWidth);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setHorizontalSpacing((int) padding);
        gridView.setVerticalSpacing((int) padding);

        gridView.setPadding((int) padding, (int) padding, (int) padding, (int) padding);

    }

    class ViewHolder {

        ImageView imageview;

        ImageView checkbox;

        int id;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ActionsUpload#getAlbumId()
     */
    @Override
    public Integer getAlbumId() {

        return 1;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.Adapters.Items.ActionsUpload#remove(com.Adapters.Items.ItemUpload)
     */
    @Override
    public void remove(ItemUpload object) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ActionsUpload#setAsCover(int)
     */
    @Override
    public void setAsCover(int id) {

        // TODO Auto-generated method stub

    }
}
